package caseManagerBusiness;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.GetRequestInputStream;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Objects;

/**
 * Created by yangxianming on 2018/7/26.
 */
public class QueryCase implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        //初始化返回对象。读取请求json
        //数据库查询  SELECT * FROM testcase WHERE projectName='开放平台' AND caseName LIKE "%登陆%" AND isdel='0' AND type='2';
        //{"projectName":"项目名字","caseName":"用例名字"}
        ReturnJson returnJson = new ReturnJson();
        JSONObject apiJson = GetRequestInputStream.getJson(request);
        SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
        //接受数据库出来的
        ArrayList<ArrayList<String>> re = new ArrayList<ArrayList<String>>();
        String where = " isdel='0' ";
        if (apiJson.containsKey("projectName") && !Objects.equals(apiJson.getString("projectName"), "")){
            where = where + "AND projectName LIKE \"" + apiJson.getString("projectName") + "%\" ";
        }
        if (apiJson.containsKey("caseName") && !Objects.equals(apiJson.getString("caseName"), "")){
            where = where + "AND caseName LIKE \"%" + apiJson.getString("caseName") + "%\" ";
        }
        if (apiJson.containsKey("type")){
            where = where + "AND type= \'" + apiJson.getString("type") + "\' ";
        }
        if (apiJson.containsKey("batchNo")){
            where = where + "AND batchNo= \'" + apiJson.getString("batchNo") + "\' ";
        }
        String tables = "`id`,`projectName`,`caseName`,`describe`,`data`,`isjump`,`ip`";
        SqlReturnModel sqlReturnModel = sqlDispatch.select(tables, "testcase", where, "createtime");
        if (sqlReturnModel.getIsSuccess()){
            re = sqlReturnModel.getSelectReturnData();
        }else {
            returnJson.setApiStatus(0);
            returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
        }


        JSONArray caseList = new JSONArray();
        String type = apiJson.getString("type");
        if (Objects.equals("2", type)){
            //ui用例
            for (ArrayList<String> oneCase: re){
                JSONArray a = new JSONArray();
                for (int i = 0; i < oneCase.size(); i++){
                    if (i == 4){
                        //第四列为字段data 值需要判断有没有数据
                        a.add((Objects.equals(oneCase.get(i), null))?"0":"1");
                        continue;
                    }
                    if (i == 5){
                        //isjump在uitest中暂时用不到
                        continue;
                    }
                    if (i == 6){
                        //第6列为字段ip null值返回""
                        if (Objects.equals(oneCase.get(i), null)){
                            a.add("");
                        }else {
                            a.add(oneCase.get(i));
                        }
                        continue;
                    }
                    a.add(oneCase.get(i));
                }
                caseList.add(a);
            }
        }
        if (Objects.equals("3", type)){
            //py以及其他脚本
            for (ArrayList<String> oneCase: re){
                JSONArray a = new JSONArray();
                for (int i = 0; i < oneCase.size(); i++){
                    if (i == 4){
                        //第四列为字段data 值需要判断有没有数据
                        a.add(JSON.parseObject(oneCase.get(i)).getString("caseName"));
                        continue;
                    }
                    a.add(oneCase.get(i));
                }
                caseList.add(a);
            }
        }

        returnJson.setApiStatus(1);
        returnJson.setResult(caseList);
        return returnJson.toString();
    }
}
